package de.koller.aframe.util.math;

import de.koller.oceanFramework.math.RVec2;
import java.util.Arrays;

public class AIntersectionUtil {

	private AIntersectionUtil() {}
	
	public static RVec2[] lineCircle2D( RVec2 lineA, RVec2 lineB, float r ) {
		float dx = lineB.x - lineA.x;
		float dy = lineB.y - lineA.y;
		float dr = (float)Math.sqrt( dx*dx + dy*dy );
		
		float D = lineA.x * lineB.y - lineB.x * lineA.y;
		float delta = r*r * dr*dr - D*D;
		
		if( delta < 0 ) return null;

		RVec2[] i = new RVec2[] { new RVec2(), new RVec2() };
		i[0].x = ( D * dy + sgn(dy) * dx * (float)Math.sqrt( delta ) ) / ( dr * dr );
		i[1].x = ( D * dy - sgn(dy) * dx * (float)Math.sqrt( delta ) ) / ( dr * dr );
		i[0].y = (-D * dx + Math.abs( dy ) * (float)Math.sqrt( delta ) ) / ( dr * dr );
		i[1].y = (-D * dx - Math.abs( dy ) * (float)Math.sqrt( delta ) ) / ( dr * dr );
		
		return i;
	}

	private static float sgn( float x ) {
		if( x < 0 ) return -1;
		return 1;
	}
	
	public static void main( String[] args ) {
		System.out.println(
			Arrays.toString( lineCircle2D( new RVec2( 0, 1 ), new RVec2( 0, 0 ), 1 ) )
		);
	}
}
